layui.use(['form', 'laydate'], function () {
    var form = layui.form
        ,laydate = layui.laydate
        ,$ = layui.jquery
        ,n = 10;

    //获取当先时间
    $(function() {
        var myDate = new Date;
        var year = myDate.getFullYear(); //获取当前年
        var mon = myDate.getMonth() + 1; //获取当前月
        var date = myDate.getDate(); //获取当前日
        $("#ctime").val(year + "-" + mon + "-" + date);
    });

    //选择交办时间
    $.each($('.slotTime'), function () {
        $(this).attr('lay-key', n++);
        laydate.render({
            elem: this
            ,range: true
            ,trigger: 'click'
        });
    });

    //选择通知时间
    $.each($('.noticeTime'), function () {
        $(this).attr('lay-key', n++);
        laydate.render({
            elem: this
            ,type: 'datetime'
            ,trigger: 'click'
        });
    });

    //追加
    $('.add-icon').on('click', function () {
        var appendHtml = $(this).parents('.subtitle').next().find('tbody>tr:last-child')[0].outerHTML;
        var html = "<tr>\n" +
            "            <td><input type=\"text\" name=\"delegated_leader[]\" required  lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "            <td><input type=\"text\" name=\"delegated_time[]\" readonly required lay-verify=\"\" placeholder=\"请选择\" autocomplete=\"off\" class=\"layui-input slotTime\"></td>\n" +
            "            <td><input type=\"text\" name=\"delegated_cont[]\" required lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "            <td><input type=\"text\" name=\"delegated_result[]\" required  lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "            <td><a href=\"javascript:;\" class=\"del-icon\">[ 删除 ]</a></td>\n" +
            "        </tr>";

        $(this).parents('.subtitle').next().find('tbody').append(html);

        //选择交办时间
        $.each($('.slotTime'), function () {
            $(this).attr('lay-key', n++);
            laydate.render({
                elem: this
                ,range: true
                ,trigger: 'click'
            });
        });

        //选择通知时间
        $.each($('.noticeTime'), function () {
            $(this).attr('lay-key', n++);
            laydate.render({
                elem: this
                ,type: 'datetime'
                ,trigger: 'click'
            });
        });

        form.render();
    });

    $('.add_confidential').on('click', function () {
        var html = "<tr>\n" +
            "                <td><input type=\"text\" name=\"confidential_number[]\" required  lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                <td><input type=\"text\" name=\"confidential_count[]\" required  lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                <td><input type=\"text\" name=\"confidential_receiver[]\" required lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                <td><input type=\"text\" name=\"confidential_result[]\" required  lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                <td><a href=\"javascript:;\" class=\"del-icon\">[ 删除 ]</a></td>\n" +
            "            </tr>";
        $(this).parents('.subtitle').next().find('tbody').append(html);
    });

    $('.add_bayonet').on('click', function () {
        var html = "<tr>\n" +
            "                <td><input type=\"text\" name=\"bayonet_video_company[]\" required  lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                <td><input type=\"text\" name=\"bayonet_video_query[]\" required  lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                <td><input type=\"text\" name=\"bayonet_video_transfer[]\" required  lay-verify=\"\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                <td><input type=\"text\" name=\"bayonet_video_remarks[]\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                <td><a href=\"javascript:;\" class=\"del-icon\">[ 删除 ]</a></td>\n" +
            "            </tr>";
        $(this).parents('.subtitle').next().find('tbody').append(html);
    });

    //删除
    $(document).on('click', '.del-icon', function () {
        if ($(this).parents('tbody tr').index() <= 0) {
            layer.msg('不可删除', {icon:5,anim:6});
            return false;
        } else {
            $(this).parents('tbody tr').remove();
        }
    });

    //预警选择
    form.on('radio(yujing)', function (obj) {
        if ($($(obj.elem).parents('tbody')[0]).hasClass('yujing1')) {

            $('.yujing2 .layui-input').attr('disabled', true).addClass('layui-disabled').parents('table').find('.yujing1 .layui-input').attr('disabled', false).removeClass('layui-disabled');
            $('.yujing2 .layui-select').attr('disabled', true).parents('table').find('.yujing1 .layui-select').attr('disabled', false);
            form.render();

        } else {

            $('.yujing1 .layui-input').attr('disabled', true).addClass('layui-disabled');
            $('.yujing1 .layui-select').attr('disabled', true);
            $('.yujing2 .layui-select').attr('disabled', true);
            $(this).parents('tr').find('.layui-select').attr('disabled', false);
            $(this).parents('tr').find('.layui-input').attr('disabled', false).removeClass('layui-disabled').parents('tr').siblings().find('.layui-input').attr('disabled', true).addClass('layui-disabled');
            form.render();

        }
    });

    form.on('submit(sub)',function(data){
        $.post('?',$('.layui-form').serialize(),function(res){
            var dataObj = eval('('+res+')');
            switch(dataObj.code){
                case 200: layer.alert('<div style="background:#ffffff left top no-repeat;background-size:100%;height:250px;border-radius:10px;position:relative;">' +
                    '<p style="position:absolute;left:0;bottom:20px;right:0;color:#2eb0f5;font-size:24px;text-align:center;">'+dataObj.msg+'</p>' +
                    '<a href="javascript:layer.closeAll();" id="close-href" class="layui-icon" style="width:40px;height:40px;text-align:center;line-height:40px;border-radius:50%;background-color:rgba(0,0,0,.6);color:#fff;font-weight:bold;position:absolute;left:50%;bottom:-50px;margin-left:-20px;">&#x1006;</a>' +
                    '</div>'
                    ,{
                        type: 1
                        ,title: false
                        ,closeBtn: false
                        ,area: ['70%','300px']
                        ,skin: 'temp-open'
                    }); break;
                case 404: layer.alert('<div style="background:#ffffff left top no-repeat;background-size:100%;height:250px;border-radius:10px;position:relative;">' +
                    '<p style="position:absolute;left:0;bottom:35px;right:0;color:#2eb0f5;font-size:18px;text-align:center;">'+dataObj.msg+'</p>' +
                    '<a href="javascript:layer.closeAll();" class="layui-icon" style="width:40px;height:40px;text-align:center;line-height:40px;border-radius:50%;background-color:rgba(0,0,0,.6);color:#fff;font-weight:bold;position:absolute;left:50%;bottom:-50px;margin-left:-20px;">&#x1006;</a>' +
                    '</div>'
                    ,{
                        type: 1
                        ,title: false
                        ,closeBtn: false
                        ,area: ['70%','300px']
                        ,skin: 'temp-open'
                    });
                    sub = 0;
                    break;
            }
            $('body').on('click','#close-href',function(){
                setTimeout(function(){
                    location.href="index.php";
                },500);
            });
        });
    });
});